package com.ltg.framework.mybatis.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.ltg.framework.mybatis.entity.BaseEntity;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author Nekoimi  2020/7/22 上午10:21
 * @see com.baomidou.mybatisplus.core.mapper.BaseMapper
 */
public interface PlusMapper<E extends BaseEntity> extends com.baomidou.mybatisplus.core.mapper.BaseMapper<E> {

    /**
     * 根据 entity 条件，查询一条记录
     * <p>查询一条记录，例如 qw.last("limit 1") 限制取一条记录, 注意：多条数据会报异常</p>
     *
     * @param queryWrapper 实体对象封装操作类（可以为 null）
     */
    @Override
    default E selectOne(@Param(Constants.WRAPPER) Wrapper<E> queryWrapper) {
        List<E> ts = this.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(ts)) {
            if (ts.size() != 1) {
                System.err.println("=======================================");
                System.err.println("查询单个对象出现多条！！！");
                ts.forEach(e -> System.err.println("Id: " + e.getId()));
                System.err.println("=======================================");
                throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
            }
            return ts.get(0);
        }
        return null;
    }
}
